$(function () {
    layui.use(['form', 'layer', 'jquery'],
        function () {
            $ = layui.jquery;
            let form = layui.form,
                layer = layui.layer;


            //监听提交
            form.on('submit(update)',
                function (data) {
                    //处理复选框只有一个值问题
                    let checkboxValue = new Array();
                    $("input:checkbox[name='permissionIds']:checked").each(function () { // 遍历name=standard的多选框
                        checkboxValue.push($(this).val());
                    });
                    data.field.permissionIds = checkboxValue;

                    let res = request('http://localhost:8080/admin/role/update', 'post', data.field)
                    if (res.code === 0) {
                        layer.alert("修改成功", {
                                icon: 6
                            },
                            function () {
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    } else {
                        layer.alert("修改失败", {
                                icon: 7
                            },
                            function () {
                                //关闭当前frame
                                xadmin.close();
                            })
                    }

                    return false;
                });

        });
    initCheck();

})

//初始化复选框
function initCheck() {
    layui.use("form", function () {
        let form = layui.form;
        //动态显示下拉框并回显内容
        let data = request("http://localhost:8080/admin/permission/findAll", 'get', null).data;
        let options = '';
        for (let i = 0; i < data.length; i++) {
            if (data[i].enable === 1)
                options += '<input type="checkbox" name="permissionIds" value="' + data[i].id + '" lay-skin="primary" title="' + data[i].permissionName + '">';
        }
        let permissionCheck = $("#permissionCheck");
        permissionCheck.append(options);
        form.render();
    });
    //表格初始化
    let roleId = sessionStorage.getItem('roleId')
    let res = request('http://localhost:8080/admin/role/findById', 'post', {id: roleId})
    $('#id').attr('value', res.data.id)
    $('#L_username').attr('value', res.data.roleName)
    $('#roleKey').attr('value',res.data.roleKey)
    $('#remark').attr('value', res.data.remark)
}